//
//  YWRemindListCell.m
//  maintain
//
//  Created by kyo on 17/2/7.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWRemindListCell.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
#import "UILabel+AttributedText.h"
#import "YWCommonTool.h"
#import "NSDate+Additions.h"

#import "YWReminder.h"

@interface YWRemindListCell()
/** 提醒类型图标 */
@property (nonatomic, strong) UIImageView *iconImageView;
/** 日期(工作日报显示的日期) */
@property (nonatomic, strong) UILabel *dateLabel;
/** 未读数 */
@property (nonatomic, strong) UILabel *unreadCountLabel;
/** 标题 */
@property (nonatomic, strong) UILabel *titleLabel;
/** 内容 */
@property (nonatomic, strong) UILabel *contentLabel;
/** 时间 */
@property (nonatomic, strong) UILabel *timeLabel;
@end

@implementation YWRemindListCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        [self setUpSubviews];
    }
    return self;
}

- (void)setReminder:(YWReminder *)reminder{
    _reminder = reminder;
    if (reminder.unreadCount>0) {
        self.unreadCountLabel.text = [@(reminder.unreadCount) stringValue];
        self.unreadCountLabel.hidden = NO;
    }else{
        self.unreadCountLabel.hidden = YES;
    }
    self.dateLabel.hidden = YES;
    if (reminder.type == YWReminderType_uploadFailed) {
        //离线
        self.iconImageView.image = [UIImage imageNamed:@"remind_uploadFailed"];
        self.titleLabel.text = NSLocalizedString(@"reminderTypeOfUploadFailed", nil);//@"离线任务提醒";
        self.contentLabel.text = NSLocalizedString(@"youHaveUnuploadRecord", nil);//@"您有未上传的任务";
    }else if (reminder.type == YWReminderType_missionExpiration){
        self.iconImageView.image = [UIImage imageNamed:@"remind_missionExpiration"];
        self.titleLabel.text = NSLocalizedString(@"reminderTypeOfMissionExpiration", nil);//@"任务过期提醒";
        self.contentLabel.text = reminder.content?:NSLocalizedString(@"hereWillRemindYouTheExpirationMission", nil);
    }else if (reminder.type == YWReminderType_missionChange){
        self.iconImageView.image = [UIImage imageNamed:@"remind_change"];
        self.titleLabel.text = NSLocalizedString(@"reminderTypeOfMissionChange", nil);//@"任务变更提醒";
        self.contentLabel.text = reminder.content?:NSLocalizedString(@"hereWillRemindYouTheChangedMission", nil);
    }else if (reminder.type == YWReminderType_missionCancel){
        self.iconImageView.image = [UIImage imageNamed:@"remind_cancel"];
        self.titleLabel.text = NSLocalizedString(@"reminderTypeOfMissionCancel", nil);//@"任务取消提醒";
        self.contentLabel.text = reminder.content?:NSLocalizedString(@"hereWillRemindYouTheCanceledMission", nil);
    }else if (reminder.type == YWReminderType_keepWatchNoBody){
        self.iconImageView.image = [UIImage imageNamed:@"patroling"];
        self.titleLabel.text = NSLocalizedString(@"reminderTypeOfKeepWatchNoBody", nil);//@"无人巡更提醒";
        self.contentLabel.text = reminder.content?:NSLocalizedString(@"hereWillRemindYouNoBodyKeepWatch", nil);
    }else if (reminder.type == YWReminderType_inspectionNoBody){
        self.iconImageView.image = [UIImage imageNamed:@"profile_patrol"];
        self.titleLabel.text = NSLocalizedString(@"reminderTypeOfInspectionNoBody", nil);//@"无人巡查提醒";
        self.contentLabel.text = reminder.content?:NSLocalizedString(@"hereWillRemindYouNoBodyInspection", nil);
    }else if (reminder.type == YWReminderType_dailyRport){
        self.iconImageView.image = [UIImage imageNamed:@"remind_dailyReport"];
        self.titleLabel.text = NSLocalizedString(@"reminderTypeOfDailyRport", nil);//@"工作日报";
        self.contentLabel.text = reminder.content?:NSLocalizedString(@"hereWillRemindYouTheDailyRport", nil);
        self.dateLabel.hidden = NO;
        self.dateLabel.text = @([NSDate date].day).stringValue;
    }else if (reminder.type == YWReminderType_systemNotice){
        self.iconImageView.image = [UIImage imageNamed:@"remind_systemNotice"];
        self.titleLabel.text = NSLocalizedString(@"reminderTypeOfSystemNotice", nil);//@"系统提醒";
        self.contentLabel.text = reminder.content?:NSLocalizedString(@"reminderTypeOfSystemNotice", nil);
    }else if (reminder.type == YWReminderType_appVersionNotice){
        self.iconImageView.image = [UIImage imageNamed:@"remind_appVersionNotice"];
        self.titleLabel.text = NSLocalizedString(@"reminderTypeOfAppVersionNotice", nil);//@"易维保新版本提醒";
        self.contentLabel.text = reminder.content?:NSLocalizedString(@"reminderTypeOfAppVersionNotice", nil);
    }else{
        self.iconImageView.image = [UIImage imageNamed:@"remind_replyIcon"];
        self.titleLabel.text = NSLocalizedString(@"reminderTypeOfReplyMessage", nil);//@"评论提醒";
        self.contentLabel.text = [NSString stringWithFormat:@"您有%ld%@新消息",(long)reminder.unreadCount,NSLocalizedString(@"tiao", nil)];
    }
    self.timeLabel.text = [YWCommonTool showTime:reminder.createTime/1000 andFormatter:KTimeFormatShortTimeDate];
}

- (void)setUpSubviews{
    UIView *superView = self.contentView;
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView).offset(kMarginW+5);
        make.left.mas_equalTo(superView).offset(kMarginW);
        make.width.and.height.mas_equalTo(@32);
    }];
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_iconImageView.mas_top).offset(2);
        make.left.mas_equalTo(_iconImageView.mas_left).offset(2);
        make.right.mas_equalTo(_iconImageView.mas_right).offset(-2);
        make.bottom.mas_equalTo(_iconImageView.mas_bottom).offset(-2);
    }];
    
    [self.unreadCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_iconImageView.mas_centerX).offset(32/2);
        make.centerY.mas_equalTo(_iconImageView.mas_centerY).offset(-32/2);
        make.size.mas_equalTo(CGSizeMake(2*kMarginW, 2*kMarginW));
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(_iconImageView.mas_centerY);
        make.left.mas_equalTo(_iconImageView.mas_right).offset(kMarginW+2);
        //make.right.mas_equalTo(superView.mas_right).offset(-kMarginW);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_titleLabel.mas_right).offset(kMarginW);
        make.right.mas_equalTo(superView.mas_right).offset(-kMarginW);
        make.bottom.mas_equalTo(_titleLabel.mas_bottom);
        make.width.lessThanOrEqualTo(@110);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_titleLabel.mas_bottom).offset(4);
        make.left.mas_equalTo(_titleLabel);
        make.right.mas_equalTo(superView.mas_right).offset(-kMarginW);
    }];
}

- (UIImageView*)iconImageView{
    if (!_iconImageView) {
        UIImageView * iconImageView = [[UIImageView alloc] init];
        iconImageView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:iconImageView];
        _iconImageView = iconImageView;
    }
    return _iconImageView;
}

- (UILabel *)dateLabel{
    if (!_dateLabel) {
        UILabel * label = [[UILabel alloc] init];
        label.textColor = [UIColor customBlueColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = kContentFont;
        [self.iconImageView addSubview:label];
        _dateLabel = label;
    }
    return _dateLabel;
}

- (UILabel*)unreadCountLabel{
    if (!_unreadCountLabel) {
        UILabel * label = [[UILabel alloc] init];
        label.backgroundColor = [UIColor redColor];
        label.textColor = [UIColor whiteColor];
        label.clipsToBounds = YES;
        label.layer.cornerRadius = kMarginW;
        
        label.textAlignment = NSTextAlignmentCenter;
        label.font = kContentFont;
        [self.contentView addSubview:label];
        _unreadCountLabel = label;
    }
    return _unreadCountLabel;
}

- (UILabel*)titleLabel{
    if (!_titleLabel) {
        UILabel * label = [[UILabel alloc] init];
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentLeft;
        label.font = KTitleFont;
        [self.contentView addSubview:label];
        _titleLabel = label;
    }
    return _titleLabel;
}

- (UILabel*)contentLabel{
    if (!_contentLabel) {
        UILabel * label = [[UILabel alloc] init];
        label.textColor = [UIColor customTextGrayColor];
        label.textAlignment = NSTextAlignmentLeft;
        label.font = kContentFont;
        [self.contentView addSubview:label];
        _contentLabel = label;
    }
    return _contentLabel;
}

- (UILabel *)timeLabel{
    if (!_timeLabel) {
        UILabel * label = [[UILabel alloc] init];
        label.textColor = [UIColor customTextGrayColor];
        label.textAlignment = NSTextAlignmentRight;
        label.font = KSmallContentFont;
        [self.contentView addSubview:label];
        _timeLabel = label;
    }
    return _timeLabel;
}

@end
